package clock._11.krylova;


import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.GregorianCalendar;

public class ClockPanel extends JPanel {
    protected Graphics2D hourArr;
    protected Graphics2D minArr;
    protected Graphics2D secArr;


    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2D = (Graphics2D) g;
        g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        int d = 300;
        g2D.translate(getWidth() / 2, getHeight() / 2);
//Циферблат

        Graphics2D oval = (Graphics2D) g2D.create();
        InputStream lemon = ClockPanel.class.getResourceAsStream("pics/lemon copy.png");
        Image ovalLem = null;
        try {
            ovalLem = ImageIO.read(lemon);
        } catch (IOException e) {
            System.out.println("Cannot find lemon!");
        }
        assert ovalLem != null;
        oval.drawImage(ovalLem, -ovalLem.getWidth(this) / 2, -ovalLem.getHeight(this) / 2, null);

//Время
        GregorianCalendar gc = new GregorianCalendar();
        int hour = gc.get(GregorianCalendar.HOUR);
        int min = gc.get(GregorianCalendar.MINUTE);
        int sec = gc.get(GregorianCalendar.SECOND);

//Стрелочки
        this.hourArr = (Graphics2D) g2D.create();
        this.hourArr.setColor(Color.black);
        this.hourArr.setStroke(new BasicStroke(3));
        this.hourArr.rotate(hour * Math.PI / 6);
        this.hourArr.drawLine(0, 0, 0, -d / 4);
        this.minArr = (Graphics2D) g2D.create();
        this.minArr.setColor(Color.black);
        this.minArr.setStroke(new BasicStroke(2));
        this.minArr.rotate(min * Math.PI / 30);
        this.minArr.drawLine(0, 0, 0, -d / 3);
        this.secArr = (Graphics2D) g2D.create();
        this.secArr.setColor(Color.black);
        this.secArr.setStroke(new BasicStroke(1));
        this.secArr.rotate(sec * Math.PI / 30);
        this.secArr.drawLine(0, 0, 0, -d / 2);

        double angle = Math.PI / 6;
        for (int i = 1; i <= 12; i++) {
            Graphics2D numbs = (Graphics2D) g.create();
            numbs.rotate(angle);
            numbs.translate(0, -d / 2 + d / 12); //+19 was
            numbs.rotate(-angle);
            numbs.setColor(Color.BLUE);
            drawCenteredString(numbs, "" + i, 0, 0, getFont().deriveFont((float) 12.0));
            angle = angle + Math.PI / 6;
        }

        double angle2 = 0;
        for (int i = 0; i <= 59; i++) {
            Graphics2D minStr = (Graphics2D) g.create();
            minStr.rotate(angle2);
            if (i % 5 == 0) {
                minStr.setStroke(new BasicStroke(2));
                minStr.drawLine(0, d / 2 - 10, 0, d / 2);
            } else {
                minStr.setStroke(new BasicStroke(1));
                minStr.drawLine(0, d / 2 - 6, 0, d / 2);
            }
            angle2 = angle2 + Math.PI / 30;
        }

    }

    private void drawCenteredString(Graphics2D g, String s, int x0, int y0, Font font) {
        FontMetrics fm = g.getFontMetrics(font);
        x0 -= fm.stringWidth(s) / 2;
        y0 -= fm.getHeight() / 2;
        g.drawString(s, x0, y0 + fm.getAscent());
    }
}